package com.tianyi.brand;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tianyi.dao.entity.brand.Brand;
import com.tianyi.dao.entity.brand.BrandExample;
import com.tianyi.dao.mapper.BrandMapper;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.BrandReq;
import com.tianyi.sysUser.SysUserService;
import com.tianyi.vo.PullDownVO;
import com.tianyi.vo.brand.BrandVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 品牌service
 *
 * @author cuixuhui
 * @date 2020/06/04
 */
@Service
public class BrandService {

    private static final Logger logger = LoggerFactory.getLogger(BrandService.class);

    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private SysUserService sysUserService;

    public WebResult<String> preCheck(BrandReq req){

        if (StringUtils.isBlank(req.getBrandName())){
            return WebResult.error("品牌名称不能为空");
        }
        if (StringUtils.isBlank(req.getBrandCode())){
            return WebResult.error("品牌code不能为空");
        }

        BrandExample example = new BrandExample();
        example.createCriteria().andBrandNameEqualTo(req.getBrandName());
        example.or().andBrandCodeEqualTo(req.getBrandCode());
        List<Brand> brands = brandMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(brands)){
            return WebResult.error("品牌已存在");
        }

        return WebResult.ok("success");
    }

    public WebResult<String> addBrand(BrandReq req) {

        WebResult<String> preCheck = preCheck(req);
        if (!preCheck.isSuccess()){
            return preCheck;
        }

        Brand brand = req2Brand(req, null);
        int i = brandMapper.insert(brand);

        return  WebResult.ok("success");
    }

    public WebResult<String> deleteBrandById(BrandReq req) {

        if (Objects.isNull(req.getId())){
            return WebResult.error("id 不能为空");
        }
        Brand brand = queryBrandByBrandId(req.getId());
        if (Objects.isNull(brand)){
            return WebResult.error("品牌不存在");
        }

        brandMapper.deleteByPrimaryKey(brand.getId());

        return  WebResult.ok("success");
    }


    public Brand req2Brand(BrandReq req, Brand updateBrand){

        Brand brand = updateBrand;
        if (Objects.isNull(brand)){
            brand = new Brand();
            brand.setGmtCreate(new Date());
        }
        brand.setGmtModified(new Date());

        brand.setBrandName(req.getBrandName());
        brand.setBrandCode(req.getBrandCode());
        brand.setBrandContent(req.getBrandContent());

        brand.setOperator(req.getEmpId());
        return brand;
    }

    public WebResult<String> updateBrand(BrandReq req) {

        if (Objects.isNull(req.getId())){
            return WebResult.error("id 不能为空");
        }
        WebResult<String> preCheck = preCheck(req);
        if (!preCheck.isSuccess()){
            return preCheck;
        }
        Brand brand = queryBrandByBrandId(req.getId());
        if (Objects.isNull(brand)){
            return WebResult.error("品牌不存在");
        }
        brand = req2Brand(req, brand);
        int i = brandMapper.updateByPrimaryKeySelective(brand);

        return WebResult.ok("success");
    }

    public WebResult<PageInfo<BrandVO>> queryBrandPage(BrandReq req) {

        BrandExample example = new BrandExample();
        example.setOrderByClause("id DESC");
        BrandExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(req.getBrandName())){
            criteria.andBrandCodeEqualTo(req.getBrandName());
        }
        if (StringUtils.isNotBlank(req.getBrandCode())){
            criteria.andBrandCodeEqualTo(req.getBrandCode());
        }

        Page<Brand> brandPage = brandMapper.selectByExample(example, new RowBounds(req.getPageNo(), req.getPageSize()));
        PageInfo<Brand> brandPageInfo = brandPage.toPageInfo();
        List<Brand> brandPageInfoList = brandPageInfo.getList();

        PageInfo<BrandVO> pageInfo = new PageInfo();
        BeanUtils.copyProperties(brandPageInfo, pageInfo);
        if (CollectionUtils.isEmpty(brandPageInfoList)){
            return WebResult.ok(pageInfo);
        }

        List<Long> empIds = brandPageInfoList.stream().map(Brand::getOperator).collect(Collectors.toList());
        Map<Long, String> sysUserNameMap = sysUserService.queryUserNameMapByUserIds(empIds);

        List<BrandVO> brandVOS = brandPageInfoList.stream().map(ret -> {
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(ret, brandVO);
            brandVO.setOperatorCn(sysUserNameMap.get(ret.getOperator()));
            return brandVO;
        }).collect(Collectors.toList());
        pageInfo.setList(brandVOS);
        return WebResult.ok(pageInfo);
    }

    /**
     * 获取品牌名称翻译
     * @param brandIds
     */
    public Map<Long, String> queryBrandNameMapByBrandIds(List<Long> brandIds) {

        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(brandIds);
        List<Brand> brands = brandMapper.selectByExample(example);
        Map<Long, String> brandIdNameMap = ListUtils.emptyIfNull(brands).stream().collect(Collectors.toMap(Brand::getId, Brand::getBrandName, (oldOne, newOne) -> newOne));
        return brandIdNameMap;
    }

    /**
     * 查找品牌
     *
     * @param brandId
     */
    public Brand queryBrandByBrandId(Long brandId) {
        if (Objects.isNull(brandId)){
            return null;
        }
        return brandMapper.selectByPrimaryKey(brandId);
    }

    /**
     * 获取所有品牌的下拉
     * @param req
     * @return
     */
    public WebResult<List<PullDownVO>> getBrandPullDown(BrandReq req) {

        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        List<Brand> brands = brandMapper.selectByExample(example);
        List<PullDownVO> pullDownVOS = ListUtils.emptyIfNull(brands).stream().map(ret -> {
            PullDownVO vo = new PullDownVO();
            vo.setLabel(ret.getBrandName());
            vo.setValue(ret.getId());
            return vo;
        }).collect(Collectors.toList());
        return WebResult.ok(pullDownVOS);
    }
}
